/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class OrderProductModel extends AbstractModel
{
	protected List<AttributeValueModel> attributes;
	protected List<DownloadModel> downloads;
	protected int opId;
	protected int id;
	protected String model;
	protected String name;
	protected double basePrice;
	protected double finalPrice;
	protected double tax;
	protected int quantity;
	protected double weight;
	protected int taxClassId;

	public OrderProductModel()
	{
		attributes = new ArrayList<AttributeValueModel>();
		downloads = new ArrayList<DownloadModel>();
		opId = 0;
		id = 0;
		model = "";
		name = "";
		basePrice = 0.0;
		finalPrice = 0.0;
		tax = 0.0;
		quantity = 0;
		weight = 0.0;
		taxClassId = 0;
	}
	
	public OrderProductModel(JSONObject jsonObject)
	{
		attributes = new ArrayList<AttributeValueModel>();
		downloads = new ArrayList<DownloadModel>();
		
		opId = convertToInteger(jsonObject,"op_id");
		id = convertToInteger(jsonObject,"id");
		model = convertToString(jsonObject,"model");
		name = convertToString(jsonObject,"name");
		basePrice = convertToDouble(jsonObject,"base_price");
		finalPrice = convertToDouble(jsonObject,"final_price");
		tax = convertToDouble(jsonObject,"tax");
		quantity = convertToInteger(jsonObject,"quantity");
		weight = convertToDouble(jsonObject,"weight");
		taxClassId = convertToInteger(jsonObject,"tax_class_id");
		
		buildAttributes(jsonObject);
		buildDownloads(jsonObject);
	}
	
	protected void buildAttributes(JSONObject jsonObject)
	{
		getAttributes().clear();
		try {
			JSONValue attributeValue = jsonObject.get("attributes");

			if (attributeValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = attributeValue.isArray()) != null)
				{
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonAObject;
						if ((jsonAObject = jsonValue.isObject()) != null)
						{
							AttributeValueModel avModel = new AttributeValueModel(jsonAObject);
							addAttribute(avModel);
						}
					}
				}
			}
		}
		catch (Exception e) {};
	}
	
	protected void buildDownloads(JSONObject jsonObject)
	{
		downloads.clear();
		try {
			JSONValue downloadValue = jsonObject.get("downloads");

			if (downloadValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = downloadValue.isArray()) != null)
				{
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonAObject;
						if ((jsonAObject = jsonValue.isObject()) != null)
						{
							DownloadModel dlModel = new DownloadModel(jsonAObject);
							downloads.add(dlModel);
						}
					}
				}
			}
		}
		catch (Exception e) {};
	}
	
	public void update(OrderProductModel opModel)
	{
		opId = opModel.getOpId();
		id = opModel.getId();
		model = opModel.getModel();
		name = opModel.getName();
		basePrice = opModel.getBasePrice();
		finalPrice = opModel.getFinalPrice();
		tax = opModel.getTax();
		quantity = opModel.getQuantity();
		attributes = opModel.getAttributes();
	}

	public double getBasePrice() {
		return basePrice;
	}

	public double getFinalPrice() {
		return finalPrice;
	}

	public int getId() {
		return id;
	}

	public String getModel() {
		return model;
	}

	public String getName() {
		return name;
	}

	public int getOpId() {
		return opId;
	}

	public int getQuantity() {
		return quantity;
	}

	public double getTax() {
		return tax;
	}

	public double getWeight() {
		return weight;
	}

	public int getTaxClassId() {
		return taxClassId;
	}

	public List<AttributeValueModel> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<AttributeValueModel> attributes) {
		this.attributes = attributes;
	}

	public void addAttribute(AttributeValueModel attribute) {
		attributes.add(attribute);
	}

	public List<DownloadModel> getDownloads() {
		return downloads;
	}

}
